package com.bsj.travel.config.rocketmq.producer;

import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.context.annotation.Bean;

/**
 * @author yinhao
 * @version 1.0
 * @description RocketMQ消息配置类
 * @date 2023/11/17
 */
@Slf4j
@RefreshScope
public class RocketMqConfig {

    /**
     * 消息服务地址
     */
    @Value("${rocketmq.name-server}")
    private String nameServer;
    /**
     * 生产者组名，规定一个应用里面唯一
     */
    @Value("${rocketmq.producer.group}")
    private String producerGroup;
    /**
     * 消息发送超时时间，单位ms
     */
    @Value("${rocketmq.producer.send-message-timeout}")
    private Integer sendMsgTimeout;
    /**
     * 消息发送失败重试次数
     */
    @Value("${rocketmq.producer.retry-times-when-send-failed}")
    private Integer retryTimesWhenSendFailed;
    /**
     * 异步消息发送失败重试次数
     */
    @Value("${rocketmq.producer.retry-times-when-send-async-failed}")
    private Integer retryTimesWhenSendAsyncFailed;

    @Bean
    public DefaultMQProducer baseMQProducer() throws Exception {
        DefaultMQProducer producer = new DefaultMQProducer();
        producer.setNamesrvAddr(this.nameServer);
        producer.setProducerGroup(this.producerGroup);
        producer.setSendMsgTimeout(this.sendMsgTimeout);
        producer.setRetryTimesWhenSendFailed(this.retryTimesWhenSendFailed);
        producer.setRetryTimesWhenSendAsyncFailed(this.retryTimesWhenSendAsyncFailed);
        producer.start();
        log.info("producer is start ! groupName:[{}],namesrvAddr:[{}]"
                , producerGroup, nameServer);
        return producer;
    }
}
